﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ListenMusic.AdminAlbum_ServiceReference;
using System.ServiceModel;
using ListenMusic.Models.ViewModel;
namespace ListenMusic.Models.Reposistory.Admin
{
    public class ManageAlbumReposistory
    {
        ManageAlbumServiceClient clientManageAlbum;
        public List<AlbumSinger> getAllAlbum()
        {
            clientManageAlbum = new ManageAlbumServiceClient();
            clientManageAlbum.ClientCredentials.UserName.UserName = "admin";
            clientManageAlbum.ClientCredentials.UserName.Password = "123456";
            try {
                AlbumSinger[] album = clientManageAlbum.getAllAlbum();
                return album.ToList();
            }
            catch (FaultException e) { throw e; }
        }       
        public int countSonginAlbum(int albumid)
        {
            clientManageAlbum = new ManageAlbumServiceClient();
            clientManageAlbum.ClientCredentials.UserName.UserName = "admin";
            clientManageAlbum.ClientCredentials.UserName.Password = "123456";
            try
            {
                return clientManageAlbum.countSonginAlbum(albumid);
            }
            catch (FaultException e) { throw e; }
        }
        public bool insertNewAlbum(Album a)
        {
            clientManageAlbum = new ManageAlbumServiceClient();
            clientManageAlbum.ClientCredentials.UserName.UserName = "admin";
            clientManageAlbum.ClientCredentials.UserName.Password = "123456";
            try
            {
                return clientManageAlbum.insertNewAlbum(a);
            }
            catch (FaultException e) { throw e; }
        }
        public int getMax_AlbumID()
        {
            clientManageAlbum = new ManageAlbumServiceClient();
            clientManageAlbum.ClientCredentials.UserName.UserName = "admin";
            clientManageAlbum.ClientCredentials.UserName.Password = "123456";
            try
            {
                return clientManageAlbum.getMax_AlbumID();
            }
            catch (FaultException e) { throw e; }
        }
        public bool uploadAlbumSong(Album_Song albumSong)
        {
            clientManageAlbum = new ManageAlbumServiceClient();
            clientManageAlbum.ClientCredentials.UserName.UserName = "admin";
            clientManageAlbum.ClientCredentials.UserName.Password = "123456";
            try
            {
                return clientManageAlbum.uploadAlbumSong(albumSong);
            }
            catch (FaultException e) { throw e; }
        }
        public int getMaxID_AlbumSongID()
        {
            clientManageAlbum = new ManageAlbumServiceClient();
            clientManageAlbum.ClientCredentials.UserName.UserName = "admin";
            clientManageAlbum.ClientCredentials.UserName.Password = "123456";
            try
            {
                return clientManageAlbum.getMaxID_AlbumSong();
            }
            catch (FaultException e) { throw e; }
        }
        public AlbumSinger getAlbum_byID(int albumid)
        {
            clientManageAlbum = new ManageAlbumServiceClient();
            clientManageAlbum.ClientCredentials.UserName.UserName = "admin";
            clientManageAlbum.ClientCredentials.UserName.Password = "123456";
            try
            {
                AlbumSinger aa = clientManageAlbum.getAlbum_byID(albumid);
                return aa;
            }
            catch (FaultException e) { throw e; }
        }
        public bool deleteSonginAlbum(int songid)
        {
            clientManageAlbum = new ManageAlbumServiceClient();
            clientManageAlbum.ClientCredentials.UserName.UserName = "admin";
            clientManageAlbum.ClientCredentials.UserName.Password = "123456";
            try
            {
                return clientManageAlbum.deleteSonginAlbum(songid);
            }
            catch (FaultException e) { throw e; }
        }
        public UpdateAlbumPage loadUpdateAlbumPage(int albumid)
        {
            try
            {
                AlbumSinger aa = getAlbum_byID(albumid);
                UpdateAlbumPage uap = new UpdateAlbumPage();
                uap.AlbumID = aa.AlbumID;
                uap.AlbumName = aa.AlbumName;
                uap.CategoryID = aa.CategoryID;
                uap.CountryID = aa.CountryID;
                uap.CountryName = aa.CountryName;
                uap.Description = aa.Description;
                uap.SingerID = aa.SingerID;
                uap.ReleaseYear = aa.ReleaseYear;
                uap.Picture = aa.Picture;
                uap.SingerName = aa.SingerName;
                ManageSongReposistory _song = new ManageSongReposistory();
                uap.lstsongAlbum = _song.getSongAlbum_byAlbumID(albumid);
                return uap;
            }
            catch (FaultException e) { return null; throw e; }
        }
        public bool updateAlbum(Album album)
        {
            clientManageAlbum = new ManageAlbumServiceClient();
            clientManageAlbum.ClientCredentials.UserName.UserName = "admin";
            clientManageAlbum.ClientCredentials.UserName.Password = "123456";
            try
            {
                return clientManageAlbum.updateAlbum(album);
            }
            catch (FaultException e) { return false; throw e; }
        }
    }
}